<template>
  <div>
    <el-table
      :data="tableData"
      :border="true"
    >
      <el-table-column
        prop="boxId"
        label="箱唛"
        align="center"
        min-width="150"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="boxType"
        label="货箱类型"
        align="center"
        min-width="150"
      >
        <template v-slot="{row}">
          {{getBoxTypeStr(row.boxType)}}
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.ListingIDAndSKU')"
        align="center"
        min-width="150"
      >
        <template slot-scope="{row}">
          {{ $t('shipment.Not labeled') }} {{row.fromSku ? `(${row.fromSku})` : ''}}
        </template>
      </el-table-column>
      <el-table-column
        prop="toInventoryId"
        label="新产品标"
        min-width="150"
        align="center"
        show-overflow-tooltip
      >
        <template v-slot="{row}">
          <template v-if="row.toInventoryId">
            {{row.toInventoryId}}
          </template>
          <template v-else-if="row.toSuitId">
            {{row.toSku}}
          </template>
          <template v-else>
            -
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="exGoodsNum"
        label="商品数量"
        align="center"
        min-width="150"
      ></el-table-column>
      <template v-if="data.status !== 2">
        <el-table-column
          prop="op"
          label="操作"
          align="center"
          min-width="200"
        >
          <template v-slot="{row}">
            <template v-if="!row.toSuitId">
              <el-button type="primary" @click="chooseLabel(row)">选择产品标</el-button>
            </template>
            <template v-else>
              <el-button type="warning" @click="chooseLabel(row)">修改产品标</el-button>
            </template>
          </template>
        </el-table-column>
      </template>
      <template v-else>
        <el-table-column
          prop="rlGoodsNum"
          label="实际贴标数量"
          align="center"
          min-width="150"
        >
          <template v-slot="{row}">
            <span :class="{
            'font-red': row.rlGoodsNum !== row.exGoodsNum,
            }">
              {{row.rlGoodsNum}}
            </span>
          </template>

        </el-table-column>
      </template>


    </el-table>


    <el-dialog
      title="选择产品标"
      :visible.sync="dialog"
    >
      <div>
        <div class="mgb10">请选择该箱产品需要粘贴的产品标签</div>
        <el-table
          :data="dialogTable"
          border
          v-loading="dialogLoading"
        >
          <el-table-column
            prop=""
            label="选择"
            width="100"
            align="center"
          >
            <template v-slot="{row}">
              <el-radio v-model="chooseSuitListId" :label="row.suitListId" :disabled="row.isGoods && !row.labelPdf">
                <span></span>
              </el-radio>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('table.ListingIDAndSKU')"
            align="center"
            min-width="150"
          >
            <template slot-scope="{row}">
              <template v-if="row.isGoods">
                {{row.goodsId}} {{row.sku ? `(${row.sku})` : ''}}
              </template>
              <template v-else>
                {{$t('shipment.System label')}} ({{row.sku}})
              </template>
            </template>
          </el-table-column>
          <el-table-column
            label="产品标"
            align="center"
            min-width="150"
            prop="inventoryId"
          >
          </el-table-column>
          <el-table-column
            prop="accountName"
            label="店铺"
            min-width="100"
            align="center"
            show-overflow-tooltip
          >
            <template v-slot="{row}">
              {{row.accountName || '-'}}
            </template>
          </el-table-column>
          <el-table-column
            prop="label"
            label="官方产品标"
            min-width="150"
            align="center"
          >
            <template v-slot="{row}">
              <template v-if="row.isGoods">
                <upload-goods-label
                  :id="row.suitListId"
                  :file="row.labelPdf"
                  @uploadFile="uploadPDFSuccess"
                ></upload-goods-label>
              </template>
              <template v-else>
                -
              </template>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <template #footer>
        <el-button @click="dialog = false">取消</el-button>
        <el-button type="primary" @click="confirmDialog">确认</el-button>
      </template>

    </el-dialog>
  </div>

</template>

<script>
import {getGoodsInventoryBySuitId} from "@/api/labelOrder";
import UploadGoodsLabel from "@/views/stock/components/UploadGoodsLabel.vue";
import {getBoxTypeStr} from "@/utils/boxLabel";

export default {
  name: 'LabelOrderInfoTable1',
  components: {UploadGoodsLabel},
  props: {
    tableData: {
      type: Array,
      required: true,
      default: () => []
    },
    data: {
      type: Object,
      required: true,
      default: () => {}
    }
  },
  computed: {
    chooseRow(){
      return this.dialogTable.find(item => item.suitListId === this.chooseSuitListId)
    }
  },
  data(){
    return {
      dialog: false,
      dialogTable: [],
      dialogForm: {
        id: 0,
      },
      dialogLoading: false,
      currentRow: null,
      chooseSuitListId: 0,
    }
  },
  methods: {
    getBoxTypeStr,
    chooseLabel(row){
      this.dialog = true
      this.currentRow = row
      this.dialogLoading = true
      this.chooseSuitListId = 0
      getGoodsInventoryBySuitId(row.fromSuitId)
        .then(res=>{
          this.dialogLoading = false
          this.dialogTable = res.data
          if(this.currentRow.toSuitId){
            const obj = this.dialogTable.find(item => {
              if(this.currentRow.toGoodsIsSystem){
                return this.currentRow.toSystemLabelId === item.systemLabelId
              }else{
                return this.currentRow.toSuitListId === item.suitListId
              }
            })
            if(obj){
              this.chooseSuitListId = obj.suitListId
            }
          }
        })
    },
    uploadPDFSuccess(info){
      this.dialogTable.some(item=>{
        if(item.suitListId === info.id){
          item.labelPdf = info.labelPdf
          return true
        }
        return false
      })
    },
    changeRow(row){

    },
    // 确认提交表单
    confirmDialog(){
      if(this.chooseRow.isGoods){
        this.currentRow.toSuitListId = this.chooseRow.suitListId;
        this.currentRow.toSuitId = this.chooseRow.suitId;
        this.currentRow.toGoodsId = this.chooseRow.goodsId;
        this.currentRow.toSku = this.chooseRow.sku;
        this.currentRow.toGoodsIsSystem = false;
        this.currentRow.toSystemLabelId = 0;
        this.currentRow.toInventoryId = this.chooseRow.inventoryId;
      }else{
        this.currentRow.toSuitListId = this.chooseRow.suitListId;
        this.currentRow.toSuitId = this.chooseRow.suitId;
        this.currentRow.toGoodsId = this.chooseRow.goodsId;
        this.currentRow.toSku = this.chooseRow.sku;
        this.currentRow.toGoodsIsSystem = true;
        this.currentRow.toSystemLabelId = this.chooseRow.systemLabelId;
        this.currentRow.toInventoryId = '';
      }
      this.dialog = false

      // 判断是否有相同的商品
      const otherGoods = this.tableData.filter(item => item.fromSuitId === this.currentRow.fromSuitId && !item.toSuitId)
      if(otherGoods.length === 0){
        return;
      }
      new Promise((resolve, reject)=>{
        this.$confirm('该商品的其他箱唛尚未选择产品标，是否贴相同的产品标？', '存在未贴标相同的商品')
          .then(() => {
            resolve()
          })
          .catch(() => {
            reject()
          })
      })
        .then(()=>{
          otherGoods.forEach(item=>{
            if(this.chooseRow.isGoods){
              item.toSuitListId = this.chooseRow.suitListId;
              item.toSuitId = this.chooseRow.suitId;
              item.toGoodsId = this.chooseRow.goodsId;
              item.toSku = this.chooseRow.sku;
              item.toGoodsIsSystem = false;
              item.toSystemLabelId = 0;
              item.toInventoryId = this.chooseRow.inventoryId;
            }else{
              item.toSuitListId = this.chooseRow.suitListId;
              item.toSuitId = this.chooseRow.suitId;
              item.toGoodsId = this.chooseRow.goodsId;
              item.toSku = this.chooseRow.sku;
              item.toGoodsIsSystem = true;
              item.toSystemLabelId = this.chooseRow.systemLabelId;
              item.toInventoryId = '';
            }
          })
        })
        .catch(()=>{

        })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
